package cn.bsuc.crm.controller;


import cn.bsuc.crm.dto.LoginFormDTO;
import cn.bsuc.crm.dto.UserDTO;
import cn.bsuc.crm.entity.User;
import cn.bsuc.crm.service.UserService;
import cn.bsuc.crm.util.Code;
import cn.bsuc.crm.util.Result;
import cn.bsuc.crm.vo.UserVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    public UserService userService;

    /**
     * 登录
     *
     * @param loginFormDTO
     * @return
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginFormDTO loginFormDTO,HttpServletRequest request) {
        return userService.login(loginFormDTO);
    }

    /**
     * 修改用户密码
     *
     * @param request
     * @return
     */
    @ApiOperation("修改用户密码")
    @PostMapping("/updateUserPassword")
    public Result updateUserPassword(@RequestBody Map<String,String> map, HttpServletRequest request) {

        return userService.updateUserPassword(map.get("password"),request);
    }

    //保存用户
    @PostMapping("/saveUser")
    public Result save(@RequestBody UserDTO userDTO){
        int i = userService.saveUser(userDTO,userDTO.getRoleId());
        if (i == 1){
            return Result.ok();
        }else if (i == 0){
            return  Result.fail(Code.ERROR,"保存失败");
        }else {
            return Result.fail(Code.SYSTEM_ERR,"系统出错");
        }
    }
        //查询所有用户及用户的多字段查询
    @GetMapping("/selectAllUser")
    public Result selectAllUser(String name,String phone,String email,@RequestParam(defaultValue = "1") Integer pageNo,
                                @RequestParam(defaultValue = "3")Integer pageSize){
        if(name!=null || phone!=null || email!=null ){
            User user = new User();
            user.setUserName(name);
            user.setEmail(email);
            user.setPhone(phone);
            Page<User> userPage = userService.selectByParams(user, pageNo,pageSize);
            return Result.ok(userPage);
        } else {
            Page<User> userPage = userService.selectAllUser(pageNo, pageSize);
            return Result.ok(userPage);
        }
    }

    //查询所有用户
    @RequestMapping("/selectAllUser")
    public Result selectAllUser(@RequestParam(defaultValue = "1") Integer pageNo,
                                @RequestParam(defaultValue = "3") Integer pageSize) {
        Page<User> userPage = userService.selectAllUser(pageNo, pageSize);
            return Result.ok(userPage);
    }

    //查询所有用户
    @GetMapping("/selectUser")
    public Result selectUser() {
        List<User> list = userService.list();
        return Result.ok(list);
    }

    //查询所有用户以及角色
    @GetMapping("/selectAllUserRole")
    public Result selectAllUserRole(Integer userId){
        UserVO userVO = userService.selectAllUserRole(userId);
        if (userVO != null){
            return Result.ok(userVO);
        }else if (userVO == null){
            return  Result.fail(Code.ERROR,"查询失败");
        }else {
            return Result.fail(Code.SYSTEM_ERR,"系统出错");
        }
    }

    //删除一个用户及角色
    @GetMapping("/deleteUserAndRole")
    public Result deleteUserAndRole(Integer userId){
        int i = userService.deleteUserAndRole(userId);
        if (i>0 ){
            return Result.ok();
        }else if (i <= 0){
            return  Result.fail(Code.ERROR,"删除失败");
        }else {
            return Result.fail(Code.SYSTEM_ERR,"系统出错");
        }
    }

    //批量删除用户及角色
    @RequestMapping("/deleteSomeUser")
    public Result deleteSomeUser(Integer[] userId){
        System.out.println(userId);
        userService.deleteSomeUser(userId);
            return Result.ok();
        }
}

